<template>
  <el-row v-loading="loading">

    <el-col :span="24">
      <evalution-suggestion-edit ref="evalutionSuggestionEdit" v-on:save-finished="getEvalutionSuggestionList()"></evalution-suggestion-edit >
      <role-permission ref="rolePermission"></role-permission>
      <el-row class="search-row">
        <el-col :span="12">
          <el-input placeholder="评估名称" size="small" v-model="search.name">
            <el-button slot="append" icon="el-icon-search" @click="getEvalutionSuggestionList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="12" style="text-align: right;">
          <el-button type="primary" icon="el-icon-plus" size="small"
            v-show="permission.add" @click="handleCreateRole()">添加</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table :data="tEvaluateList" border size="mini" header-cell-class-name="header-row">
            <el-table-column type="selection" header-align="center" align="center">
              <template >

                <el-checkbox  ></el-checkbox>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="评估名称" header-align="center"></el-table-column>
            <el-table-column prop="result" label="评估结果" header-align="center"></el-table-column>
            <el-table-column prop="advise" label="建议" width="500px" header-align="center"></el-table-column>
            <el-table-column prop="" label="创建时间" header-align="center">
              <template slot-scope="scope">
              {{scope.row.createTime | dateFormat}}
            </template>
            </el-table-column>
            <el-table-column prop="state" label="状态" header-align="center" align="center">
              <template slot-scope="scope">
                <span v-bind:class="{ 'role-locked': scope.row.state, 'role-not-locked': !scope.row.state }">{{ scope.row.state ? "禁用" : "可用" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="260">
              <template slot-scope="scope">
                <el-button v-show="permission.edit" type="primary" size="mini"
                  @click="handleEditEvaluate(scope.$index, scope.row)">编辑</el-button>
                <el-button v-show="permission.remove" type="danger" size="mini"
                  @click="handleDeleteRole(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.currentPage"
            :page-sizes="[5, 10, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tEvaluateTotal">
          </el-pagination>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { listEvalutionSuggestion,getEvaluateById,removeEvaluate } from '@/api/xy/evalutionSuggestion'
import { listResourcePermission } from "@/api/resource"
import EvalutionSuggestionEdit from "@/components/evalutionSuggestion/EvalutionSuggestionEdit"
import RolePermission from "@/components/role/rolePermission"

export default {
  data() {
    return {
      loading: false,
      permission: {
        add: true,
        edit: true,
        remove: true
      },
      search: {
        name: '',
        offset: 0,
        limit: 5,
        currentPage:1
      },
      tEvaluateTotal: 0,
      tEvaluateList: []
    }
  },
  methods: {
    getEvalutionSuggestionList() {
      this.loading = true
      listEvalutionSuggestion(this.search)
      .then(response => {
        this.tEvaluateTotal = response.data.total
        this.tEvaluateList = response.data.rows
        this.loading = false
      })
      .catch(error => {
        console.log(error)
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.search.limit = val;
      this.search.offset = (this.search.currentPage - 1) * val
      this.getEvalutionSuggestionList()
    },
    handleCurrentChange(val) {
      this.search.offset = (val - 1) * this.search.limit
      this.search.currentPage = val
      this.getEvalutionSuggestionList()
    },
    async pageInit() {
      this.loading = true
      try {
        let [listEvaluateResp, listPermissionResp] = await Promise.all([
          listEvalutionSuggestion(this.search)
          //listResourcePermission(this.$route.meta.routerId)
        ])
        this.tEvaluateTotal = listEvaluateResp.data.total
        this.tEvaluateList = listEvaluateResp.data.rows
        /*this.permission.add = listPermissionResp.data.find(item => {
          return item.resourcePermission === "roles:create"
        })
        this.permission.edit = listPermissionResp.data.find(item => {
          return item.resourcePermission === "roles:update"
        })
        this.permission.remove = listPermissionResp.data.find(item => {
          return item.resourcePermission === "roles:delete"
        })*/
        this.loading = false
      } catch(error) {
        this.outputError(error)
      }
    },
    handleCreateRole() {
      this.$refs.evalutionSuggestionEdit.$emit('openAddRoleDialog')
    },
    handleEditEvaluate(index, row) {
      this.loading = true
      getEvaluateById(row.id)
      .then(response => {
        this.$refs.evalutionSuggestionEdit.$emit('openEditRoleDialog', response.data)
        this.loading = false
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    handleDeleteRole(index, row) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        //alert('msg');
        this.loading = true
        removeEvaluate(row.id)
        .then(response => {
          this.loading = false
          this.getEvalutionSuggestionList()
        })
        .catch(error => {
          this.outputError(error)
        })
      })
      .catch(() => {})
    },
    handleSetPermission(index, row) {
      this.$refs.rolePermission.$emit('openSetPermissionDialog', row)
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
  components: { EvalutionSuggestionEdit, RolePermission },
  created() {
    this.pageInit()
  },
  filters:{
        dateFormat:function(data){
          var dt=new Date(data)
          var n=dt.getFullYear();
          var y=(dt.getMonth()+1).toString().padStart(2,"0");//补零操作
          var r=(dt.getDate()).toString().padStart(2,"0");
          dt.getHours();
          dt.getMinutes();
          dt.getSeconds();
          return n+"-"+y+"-"+r;
  }
}}
</script>

<style lang="scss" scoped>
.search-row {
  padding-bottom: 8px;
  .el-input {
    width: 260px;
  }
}

.role-locked {
  color: red;
}
.role-not-locked {
  color: green;
}
</style>
